package com.example.demo.login.controller;

import com.example.demo.login.entity.User;
import com.example.demo.login.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
public class LoginController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    private HashMap<String, Object> login(@RequestBody User user) {
        HashMap<String,Object> map = new HashMap();
        try{
            User user2 = userService.queryByName2(user);
            if(user2.getPassword().equals(user.getPassword())) {
                map.put("result","success");
                map.put("message","登录成功");
                map.put("data",user2);
                map.put("token","111");
            }else {
                map.put("result","error");
                map.put("message","密码错误");
            }
        }catch (Exception e){
            map.put("message","登录失败");
            map.put("result","error");
        }
        return map;
    }

    @CrossOrigin("http://localhost:8000")
    @RequestMapping("/getRole")
    @ResponseBody
    private HashMap<String, Object> getRole(@RequestBody User user) {
        HashMap<String,Object> map = new HashMap();
        try{
                List<String> roles = userService.getRole(user);
                map.put("result","success");
                map.put("message","获得角色成功");
                map.put("data",roles);
        }catch (Exception e){
            map.put("message","获得角色失败");
            map.put("result","error");
        }
        return map;
    }
}
